package com.thinkingstar.iads.datacenter.entity.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import javax.print.attribute.standard.MediaName;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;

import com.bstek.urule.model.rule.lhs.And;
import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.common.utils.StringUtil;
import com.thinkingstar.iads.cs.sys.entity.SysMajor;
import com.thinkingstar.iads.cs.sys.entity.SysResource;
import com.thinkingstar.iads.cs.sys.entity.SysRole;
import com.thinkingstar.iads.cs.sys.entity.SysRoleResource;
import com.thinkingstar.iads.cs.sys.entity.SysUserRole;


/**
 * @ClassName: DaoDcProjectSysUser 
 * @Description:系统中心_项目_用户信息
 * @author 1157372219@qq.com 
 * @date 2018年3月19日   
 */
@IocBean(args = {"refer:dao"})
public class DaoDcProjectSysUser extends EntityRepositoryServiceImpl<T>{
	
	private static final String ADMIN = "admin";
	private static final String SYS_MANAGEMENT = "系统管理";
	
	public DaoDcProjectSysUser(Dao dao) {
		super(dao);
	}

	/**
	 * 获取用户所属专业
	 * @param user_id
	 * @return
	 */
	public List<Integer> getMajorByUserId(long user_id){
		String executeSql="select m.* from dc_sys_user_major user_m left join dc_sys_major m on user_m.majorid=m.id where user_m.userid=@userid";
        Sql sql = Sqls.create(executeSql);
        sql.params().set("userid", user_id);
		sql.setCallback(new SqlCallback() {
			List <Integer> list = new LinkedList<>();
			@Override
			public Object invoke(Connection connection, ResultSet resultSet, Sql sql) throws SQLException {
				while(resultSet.next()){
					list.add(resultSet.getInt("id"));
				}
				return list;
			}
		});
        dao().execute(sql);
		return sql.getList(Integer.class);
	}
	
	/**
	 * 根据用户id获取用户角色信息
	 * @param user_id
	 * @return
	 */
	public List<SysRoleResource> getRoleByUserId(long user_id){
		String roleExecuteSql="select rr.* ,sr.name resName,r.name roleName from dc_sys_user_role user_r left join dc_sys_role r on user_r.roleid=r.id left join dc_sys_role_resource rr on r.id=rr.roleid left join dc_sys_resource sr on rr.resourceid=sr.id\n" +
				" where user_r.userid=@userid";
		Sql sql = Sqls.create(roleExecuteSql);
		sql.params().set("userid", user_id);
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao().getEntity(SysRoleResource.class));
		dao().execute(sql);
		return sql.getList(SysRoleResource.class);
	}
	
	
	/**
	 * 根据用户id获取用户拥有角色信息
	 * @param user_id
	 * @return
	 */
	public List <Integer> getUserRoleByUserId(long user_id){
		String user_role_sql = "select r.* from dc_sys_user_role user_r inner join dc_sys_role r ON user_r.roleid = r.id where user_r.userid = @userid ";
		Sql sql = Sqls.create(user_role_sql);
		sql.params().set("userid", user_id);
		sql.setCallback(new SqlCallback() {
			List <Integer> list = new LinkedList<>();
			@Override
			public Object invoke(Connection connection, ResultSet resultSet, Sql sql) throws SQLException {
				while(resultSet.next()){
					list.add(resultSet.getInt("id"));
				}
				return list;
			}
		});
		dao().execute(sql);
		return sql.getList(Integer.class);
	}
	
	/**
	 * 判断是否系统管理员(是否拥有管理员权限)
	 * @param user_id
	 * @return
	 */
	public Boolean is_admin(long user_id){
		String is_admin_sql = " select sur.* , sr.name roleName from dc_sys_user_role sur inner join dc_sys_role sr on sur.roleid = sr.id where sur.userid = @userid";
		Sql sql = Sqls.create(is_admin_sql);
		sql.params().set("userid", user_id);
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao().getEntity(SysUserRole.class));
		dao().execute(sql);
		List <SysUserRole> list = sql.getList(SysUserRole.class);
		for(SysUserRole sysUserRole : list){
			if(ADMIN.equals(sysUserRole.getRoleName())){
				return true;
			}
		}
		return false;
	}
	
	
	/**
	 * 根据用户id获取该用户所有资源id
	 * @param user_id
	 * @return
	 */
	public List <String> getResourceIdByUserId(long user_id){
		String user_resource_sql = " select distinct(sr.id) id "
				                 + " from dc_sys_user_role sus , dc_sys_role_resource srr , dc_sys_resource sr where sus.roleid = srr.roleid and srr.resourceid = sr.id "
				                 + " and sus.userid = @userid";
		Sql sql = Sqls.create(user_resource_sql);
		sql.params().set("userid", user_id);
		sql.setCallback(new SqlCallback() {
			List <String> list = new LinkedList<>();
			@Override
			public Object invoke(Connection connection, ResultSet resultSet, Sql sql) throws SQLException {
				while(resultSet.next()){
					list.add(resultSet.getString("id"));
				}
				return list;
			}
		});
		dao().execute(sql);
/*		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao().getEntity(String.class));
		dao().execute(sql);*/
		return sql.getList(String.class);
	}
	
	/**
	 * 根据上级资源id查询管理员的所有下级资源
	 * @return
	 */
	public List<SysResource> getResourceByAdminAndParentResId(String res_id){
		String resource_sql = " select * from dc_sys_resource where pid = @pid ";
		Sql sql = Sqls.create(resource_sql);
		sql.params().set("pid", res_id);
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao().getEntity(SysResource.class));
		dao().execute(sql);
		return sql.getList(SysResource.class);
	}

	
	/**
	 * 根据角色资源集合以及上级资源id查询资源
	 * @param id_list
	 * @param res_id
	 * @return
	 */
	public List <SysResource> getResourceByUserIdAndResId(List <String> id_list , String res_id){
		if(id_list == null || id_list.isEmpty()){
			return null;
		}
		String idString = StringUtil.getIdssplit(id_list);
		String resourceSql = " select * from dc_sys_resource where pid = @pid and id in "+idString;
		Sql sql = Sqls.create(resourceSql);
		sql.params().set("pid", res_id);
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao().getEntity(SysResource.class));
		dao().execute(sql);
		return sql.getList(SysResource.class);
	}
	
	
	/**
	 * 根据角色获取资源
	 * @param role_id
	 * @return
	 */
	public List <SysResource> getResourceByRole(long role_id){
		String resourceSql = " select a.* from dc_sys_resource a inner join dc_sys_role_resource b on a.id = b.resourceid where b.roleid = @roleid";
		Sql sql = Sqls.create(resourceSql);
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao().getEntity(SysResource.class));
		dao().execute(sql);
		return sql.getList(SysResource.class);
	}
	
	
}
